package logic

import (
	"context"
	"fmt"
	"strconv"
	"taobao-merchandise/model/mysql"
	"taobao-merchandise/pkg"

	"taobao-merchandise/taorpc/internal/svc"
	"taobao-merchandise/taorpc/taorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *taorpc.LoginRequest) (*taorpc.LoginResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	mysql.DB.Debug().Where("account=?", in.Account).Find(&user)

	if user.ID == 0 {
		return nil, fmt.Errorf("该用户不存在")
	}

	if user.Password != pkg.Md5(in.Password) {
		return nil, fmt.Errorf("密码不正确,请重新输入")
	}

	return &taorpc.LoginResponse{
		UserId: strconv.Itoa(int(user.ID)),
	}, nil
}
